// pages/device/assistant.js
const global = getApp().globalData
import {
  getDeviceProperties,
  sendCmd
} from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    device_name: '',
    device_id: '',
    result_code: '...',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      device_id: options.deviceId || wx.getStorageSync('device_id'),
      device_name: options.dname
    })
    wx.setStorageSync('device_id', options.deviceId)
    this.getProperties();
  },
  getProperties: function () {
    getDeviceProperties({
      'project_id': global.project_id || wx.getStorageSync('project_id'),
      'device_id': this.data.device_id
    }).then(res => {
      wx.stopPullDownRefresh({
        success: (res) => { },
      })
      var pro = res?.shadow[0]?.reported.properties
      pro.alarmImg = `../../images/Alarm_${pro.AlarmStatus}.png`
      pro.HumidifierStatus = pro.HumidifierStatus == "ON" ? true : false
      pro.AirCleanerStatus = pro.AirCleanerStatus == "ON" ? true : false
      pro.AlarmStatus = pro.AlarmStatus == "ON" ? true : false
      this.setData({
        property: pro,
      })

    })
  },
  switch_Humidifier: function (e) {
    let status = e.currentTarget.dataset.status
    this.setData({
      result_code: '中...'
    })
    var data = {
      "project_id": global.project_id,
      "device_id": this.data.device_id,
      "service_id": "Environment",
      "command_name": "Humidifier_Control",
      "paras": {
        "Switch": status == true ? "OFF" : "ON"
      }
    }
    this.send_cmd(data);
  },
  send_cmd: function(data){
    sendCmd(data).then(res => {
      //console.log("res",res);
      if (res.response?.result_code == 0) {
        // setTimeout(() => {
        //   this.getProperties()
        // }, 6000);
      } else {
        wx.showToast({
          title: res.errMsg || '请求异常',
          icon: 'none',
          duration: 2000,
          mask: true
        })
      }
    }, err => {
      console.log(err);
    })
  },
  switch_Aircleaner: function (e) {
    let status = e.currentTarget.dataset.status
    this.setData({
      result_code: '...'
    })
    var data = {
      "project_id": global.project_id,
      "device_id": this.data.device_id,
      "service_id": "Environment",
      "command_name": "AirCleaner_Control",
      "paras": {
        "Switch": status == true ? "OFF" : "ON"
      }
    }
    this.send_cmd(data);
  },
  switch_Alarm: function (e) {
    let status = e.currentTarget.dataset.motor
    this.setData({
      result_code: '...'
    })
    var data = {
      "project_id": global.project_id,
      "device_id": this.data.device_id,
      "service_id": "Environment",
      "command_name": "Alarm_Control",
      "paras": {
        "Switch": status == true ? "OFF" : "ON"
      }
    }
    this.send_cmd(data);
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getProperties()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 返回
  back() {
    wx.switchTab({
      url: '/pages/home/index'
    })
  }
})